package com.test.ddaidai_android.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.test.ddaidai.MyApp;
import com.test.ddaidai.BaseActivity;
import com.test.ddaidai.findpassword.FindPasswordActivity;
import com.test.ddaidai.gesture.CreateGesturePasswordActivity;
import com.test.ddaidai.gesture.UnlockGesturePasswordActivity;
import com.test.ddaidai_android.activity.R;
import com.test.ddaidai_android.register.RegisterActivity;
import com.test.widget.ValidateImageView;

public class LoginActivity extends BaseActivity implements OnClickListener {

	Button btn_lgin;
	ValidateImageView view_login_validatecode;
	String[] responseArray = null;
	TextView password_forget;
	
	SharedPreferences sp;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		setContentView(R.layout.activity_login);

		setTitle(getString(R.string.login_message),
				R.string.login_header_tv_left,true, R.string.login_header_tv_right,true);
		btn_lgin = (Button) findViewById(R.id.btn_lgin);
		btn_lgin.setOnClickListener(this);
		password_forget = (TextView) findViewById(R.id.password_forget);
		password_forget.setOnClickListener(this);
		view_login_validatecode = (ValidateImageView) findViewById(R.id.view_login_validatecode);
		view_login_validatecode.setOnClickListener(this);
		responseArray = view_login_validatecode.getValidataAndSetImage(getRandomInteger());
		//		Toast.makeText(this, getResponseStr(responseArray), 2000).show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		sp = getSharedPreferences(MyApp.DDAIDAI_SP,Context.MODE_PRIVATE);
		boolean geust_switch = sp.getBoolean("guest_switch",false);
		if(geust_switch){
			if (MyApp.getInstance().getLockPatternUtils().savedPatternExists()) {
				startActivity(new Intent(this, UnlockGesturePasswordActivity.class));
				finish();
			}
		}
	}

	public void onRightText(View view){
		//		Toast.makeText(this, "注册", Toast.LENGTH_SHORT).show();
		startActivity(new Intent(this,RegisterActivity.class));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_lgin :
			MyApp.getInstance().getLockPatternUtils().clearLock();
			Intent intent = new Intent(LoginActivity.this,
					CreateGesturePasswordActivity.class);
			// 打开新的Activity
			startActivity(intent);
			finish();
			break;
		case R.id.password_forget:
			startActivity(new Intent(this,FindPasswordActivity.class));
			break;
		case R.id.view_login_validatecode:
			responseArray = view_login_validatecode.getValidataAndSetImage(getRandomInteger());
			//			Toast.makeText(this, getResponseStr(responseArray), 2000).show();
			break;
		default:
			break;
		}
	}

	//为数组赋值1~9的随机数
	private String[] getRandomInteger(){
		String[] reuestArray = new String[4];
		for(int i = 0 ; i < 4 ; i++){
			reuestArray[i] = String.valueOf((int)(Math.random()*9+1));
		}
		return reuestArray;
	}
	//获取返回的数组
	private String getResponseStr(String[] response){
		StringBuffer stringBuffer = new StringBuffer();
		for(String str : response){
			stringBuffer.append(str);
		}
		return stringBuffer.toString();
	}

}
